package com.factory.test.m5622;

import android.os.Bundle;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityM5622RelayBinding;
import com.yecontech.sdk.base.gpio.GpioCustom;
import com.yecontech.sdk.base.gpio.YTGpio;
import com.yecontech.sdk.base.gpio.YTGpioStatusListener;

/**
 * 继电器测试 门锁测试
 *
 */
public class RelayActivity extends BaseTestActivity {

    private ActivityM5622RelayBinding binding;
    private GpioCustom onlineStatus;
    private GpioCustom ioMultiplexing;
    private GpioCustom lock1Open;

    private GpioCustom noGpio;
    private GpioCustom ncGpio;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_m5622_relay);


        noGpio = sdkService.getGpioCustomManager();
        noGpio.init("127", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.no.setChecked(activeType == 1));
            }
        });

        ncGpio = sdkService.getGpioCustomManager();
        ncGpio.init("126", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.nc.setChecked(activeType == 1));
            }
        });


        //在线状态检测， GPIO输入检测 todo 低在线，高不在线
        onlineStatus = sdkService.getGpioCustomManager();
        onlineStatus.init("130", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.onlineStatus.setChecked(activeType == 0));
            }
        });
        binding.onlineStatus.setChecked(onlineStatus.getAction() == 0);


        //开关锁
        lock1Open = sdkService.getGpioCustomManager();
        lock1Open.init("129", 0, null);
        binding.open1.setChecked(lock1Open.getAction() == 1);
        binding.open1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lock1Open.setAction(isChecked ? 1 : 0);
            }
        });


        //IO1 复用
        ioMultiplexing = sdkService.getGpioCustomManager();
        ioMultiplexing.init("80", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.ioMultiplexing.setChecked(activeType == 1));
            }
        });
        binding.ioMultiplexing.setChecked(ioMultiplexing.getAction() == 1);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
        lock1Open.release();
        onlineStatus.release();
        ioMultiplexing.release();

        noGpio.release();
        ncGpio.release();
    }
}
